# Contributor: Aiden Grossman <agrossman154@yahoo.com>
# Maintainer: Aiden Grossman <agrossman154@yahoo.com>
pkgname=freecad
pkgver=0.19.4
pkgrel=2
pkgdesc="Free and open source 3D parametric modeler"
url="https://freecadweb.org/"
license="LGPL-2.0-or-later"
arch="x86_64" # dependency OpenCascade is only x86_64
depends="
	python3 py3-pyside2 py3-six py3-pivy py3-matplotlib py3-ply py3-yaml
	py3-numpy graphviz opencascade hdf5
	"
makedepends="
	boost-dev xerces-c-dev cmake doxygen python3-dev opencascade-dev glu-dev
	vtk-dev libmedc-dev hdf5-dev eigen-dev qt5-qtxmlpatterns-dev qt5-qtsvg-dev
	qt5-qtwebengine-dev coin-dev freeimage-dev swig libtbb-dev
	"
checkdepends="xvfb-run mesa mesa-dri-gallium ttf-opensans"
source="https://github.com/FreeCAD/FreeCAD/archive/$pkgver/freecad-$pkgver.tar.gz
	vtk9.patch
	backtrace.patch
	resourceDirectory.patch
	opencascade76.patch
	tests.patch
	"
builddir="$srcdir/FreeCAD-$pkgver"

build() {
	cmake . -B ./build \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_C_FLAGS="$CFLAGS -fPIC -w" \
		-DCMAKE_CXX_FLAGS="$CXXFLAGS -fPIC -w" \
		-DCMAKE_INSTALL_PREFIX=/usr/lib/freecad \
		-DCMAKE_INSTALL_DATADIR=/usr/share/freecad \
		-DCMAKE_INSTALL_DATAROOTDIR=/usr/share \
		-DCMAKE_INSTALL_DOCDIR=/usr/share/freecad/doc \
		-DBUILD_ENABLE_CXX_STD=C++14 \
		-DBUILD_QT5=ON \
		-DFREECAD_USE_EXTERNAL_PIVY=ON \
		-DFREECAD_USE_OCC_VARIANT="Official Version" \
		-DFREECAD_USE_QT_FILEDIALOG=ON \
		-DPYTHON_EXECUTABLE=/usr/bin/python3
	cmake --build build
}

check() {
	DESTDIR=test_install cmake --install build
	mkdir -p test_install/usr/bin
	ln -s ../lib/freecad/bin/FreeCAD test_install/usr/bin/FreeCAD
	ln -s ../lib/freecad/bin/FreeCADCmd test_install/usr/bin/FreeCADCmd
	LD_LIBRARY_PATH="$PWD"/test_install/usr/lib/freecad/lib \
		xvfb-run "$PWD"/test_install/usr/bin/FreeCAD --run-test 0
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	# FreeCAD does not initialize correctly when binaries
	# are located under /usr/bin; thus, symlinks are necessary.
	install -d "$pkgdir"/usr/bin
	ln -s /usr/lib/freecad/bin/FreeCAD "$pkgdir"/usr/bin/FreeCAD
	ln -s /usr/lib/freecad/bin/FreeCADCmd "$pkgdir"/usr/bin/FreeCADCmd
}

sha512sums="
58e5e6bb4ed40ce7f3a0b653cdb0371208189b9ca266f1c29a8f9077c4e56861b3925f695718fba34781254220f8a8919a2e2a501d43573b2c733f8b21c12c1f  freecad-0.19.4.tar.gz
53f2becfebff2713c07a4a7e78a438502ea4cb401dfab116a2f84cb066f33adfaeaae9b03e9243f0534cb648cd7e3753e66456aa1e5600a0fdb724d7ef7fbf50  vtk9.patch
04e9a2cb4323aa66445eab0e27648272b84a9459f9b55784454edb74b2f3f9031749777ac4b8d01ebcae63703398d7e0033b62196461378cc9a16d7f372d5e10  backtrace.patch
a12be7920a9ba0076c45c40d4afc43e9b9a4f8370430f8be0b505c30401564c25d9ae651b840332a716d4de2f0b0ebfb61b2f4c7952b9ea6d4d2e08d3a42bfef  resourceDirectory.patch
9a1b45b032988fe26a839bd52b7c0844a47e89c0b85f5b6bbfc3d0b71581f94fe037d739fa815644bcd99e5b7a0c67abcf086343c725131764f767d2ba56aaf1  opencascade76.patch
4c8f308435fd4623bea333af80d40051a68533607525230e37ba00a84a3fae19711040ea0354f8dfb11d8d1835adb7f0a92c03680bde12d11aab08980ba09d2d  tests.patch
"
